package com.yangzb.flink.sql.extension.function;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.table.functions.TableFunction;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: yangzb
 * @date 2023/2/21 3:27 PM
 **/
@Slf4j
public class HttpFunction extends TableFunction<Map<String, String>> {

    @SneakyThrows
    public void eval(String... args) {
//        String url = "https://www.baidu.com/s?wd=" + args[0];
//        HttpRequest get = HttpUtil.createGet(url);
//        get.timeout(5000);
//        HttpResponse execute = null;
//        try {
//            execute = get.execute();
//        } catch (Exception e) {
//            log.error("",e);
//        }
        TimeUnit.MILLISECONDS.sleep(RandomUtil.randomLong(1, 10));
        Map<String, String> map = new HashMap<>();
        map.put("id", args[0]);
        map.put("name", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_PATTERN));
        collect(map);
    }

    @Override
    public TypeInformation<Map<String, String>> getResultType() {
        return Types.MAP(Types.STRING, Types.STRING);
    }
}
